import PlayerDataMgr from "../PlayerDataMgr";

export enum CoinFrom {
    SHOP,
    AD,
    TASK,
    BOX,
    TURN
}

export default class CoinSystem {

    static readonly TIP_COIN = 100;

    static coinStoreKey = "coinStoreKey"
    static coinInMomory = 0;

    static load() {
        let storedCoin = cc.sys.localStorage.getItem(this.coinStoreKey)
        console.log("[金币]加载:", storedCoin)
        if (storedCoin == null || storedCoin.length == 0) {
            this.coinInMomory = 100;
            console.log("[金币]加载1:", this.coinInMomory)
        }
        else {
            this.coinInMomory = parseInt(storedCoin);
            console.log("[金币]加载2:", this.coinInMomory)
            if (isNaN(this.coinInMomory))
                this.coinInMomory = 0;
        }
        if (PlayerDataMgr.isADVideo)
            this.coinInMomory = 1300;
    }

    static persit() {
        cc.sys.localStorage.setItem(this.coinStoreKey, this.coinInMomory);
        console.log("[金币]保存成功")
    }

    static cosumeCoin(num: number) {
        if (this.coinInMomory >= num) {
            this.coinInMomory -= num;
            this.persit();
            return true;
        }
        return false;
    }

    static addCoin(num: number) {
        this.coinInMomory += num;
        this.persit();
    }
}
